package com.sti.platform.controller;

import com.aisino.core.common.JsonPost;
import com.aisino.core.controller.BaseController;
import com.sti.platform.model.SysLog;
import com.sti.platform.service.SysLogService;
import com.sti.platform.util.logannotation.LogAnnotation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
 * 系统日志类 的控制层
 * @author zyy
 * @time 2018年5月28日 11:51:35
 */
@Controller
@RequestMapping("log")
public class SysLogController extends BaseController {

	@Autowired
	private SysLogService sysLogService;

	/**
	 * //这里的查询条件暂时定死，以后根据需要动态 改变成 SysLog sysLog 类型的
	 * @param request
	 * @param page
	 * @param row
	 * @return
	 */
	//@LogAnnotation //该注解为 日志记录的注解
	@RequestMapping("index")
	public String index(HttpServletRequest request,SysLog sysLog, String page,String row){
		List<SysLog> list = new ArrayList<>();
		page = (page==null) ? "1" : page;//防止非空传值
		row = (row==null) ? "10" : row;//防止非空传值
		list = sysLogService.getSysLogByPage(sysLog, page,row);//分页获取
		request.setAttribute("sysLogList", list);
		request.setAttribute("sysLog",sysLog);
		request.setAttribute("page",page);
		request.setAttribute("row",row);
		return "log/index";
	}


	@RequestMapping("jump")
	public String goSysLog(String id,String optType,HttpServletRequest request){
		String viewStr = "log/add";//跳转的地址
		if(id!=null && !"".equals(id)){//id 不为空是 编辑
			SysLog sysLog = sysLogService.getSysLogByID(id);
			request.setAttribute("sysLog", sysLog);
			request.setAttribute("optType", optType);
			viewStr = "log/detail";
		}
		request.setAttribute("type", optType);
		return viewStr;
	}




	@RequestMapping(value="/save")
	@ResponseBody
	public Map save(final SysLog sysLog){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				sysLogService.saveSysLog(sysLog);//保存
				map.put("message", "保存成功！" );
			}
		});
	}

	@RequestMapping(value="/update")
	@ResponseBody
	public Map update(final SysLog sysLog){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				sysLogService.updateSysLog(sysLog);//保存
				map.put("message", "修改成功！" );
			}
		});
	}


	@RequestMapping("add")
	public String add(SysLog sysLog){
		sysLogService.saveSysLog(sysLog);//保存
		return "log/index";
	}


	@RequestMapping(value="/delete")
	@ResponseBody
	public Map delete(final String id){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				sysLogService.delSysLog(id);
				map.put("message", "删除成功！");
			}
		});
	}

	@RequestMapping(value="/export")
	@ResponseBody
	public void exportExcel(Map map,SysLog SysLog,HttpServletRequest request,HttpServletResponse response){
		sysLogService.eportSysLog(response);
	}

	@RequestMapping(value="/import")
	@ResponseBody
	public void importExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response){
		sysLogService.importSysLog(file);
	}

}
